package com.mlick.a.leetcode;


/**
 * 无重复字符的最长子串
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
 */
public class LC3 {


    /**
     * 思路就是采用滑动窗口
     * 维护两个指针，左右 向前进
     */
    static class Solution {
        public int lengthOfLongestSubstring(String s) {

            if (s == null || s.length() == 0) {
                return 0;
            }

            s = s.trim();
            if (s.length() == 0) {
                return 1;
            }

            if (s.length() == 1) {
                return 1;
            }

            char[] array = s.toCharArray();

            int l = 0, max = 0;
            for (int r = 1; r < array.length; r++) {
                for (int i = l; i < r; i++) {
                    if (array[i] == array[r]) {
                        l = i + 1;
                        break;
                    }
                }
                int temp = r - l + 1;
                if (temp > max) {
                    max = temp;
                }
            }
            return max;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution().lengthOfLongestSubstring("abcabcbb")); // 3
        System.out.println(new Solution().lengthOfLongestSubstring("dvdf")); // 3
        System.out.println(new Solution().lengthOfLongestSubstring("abcabcbb")); // 3
    }


}
